package cont

import (
	"fmt"
	"github.com/labstack/echo"
	"strconv"
	errs "tacitus/library/error"
	resp "tacitus/library/response"
	"time"
)

func CreateCategoryHandler(c echo.Context) error {
	i := new(Category)
	if err := c.Bind(i); err != nil {
		return err
	}
	i.ID = 0
	if i.Type == "" || i.Name == "" {
		return errs.Called("分类的名称和类型不能为空")
	}
	if i.ParentID == 0 {
		i.Path = "/"
	} else {
		p := new(Category)
		if ok, err := x.Where("id = ?", i.ParentID).Get(p); err != nil {
			return err
		} else if !ok {
			return errs.Called("指定的上级分类不存在")
		}
		i.Path = p.Path + strconv.Itoa(p.ID) + "/"
	}
	if l, err := x.Insert(i); err != nil {
		return err
	} else if l == 0 {
		return errs.Called("插入失败")
	}
	return nil
}

func DeleteCategoryHandler(c echo.Context) error {
	i := new(struct{
		ID int `json:"id"`
		Force bool `json:"force"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	if i.ID == 0 {
		return errs.Called("分类编号不能为空")
	}
	cid := make([]int, 0)
	if err := x.Table(new(Category)).Where("path LIKE ?", "%/"+strconv.Itoa(i.ID)+"/%").Cols("id").Find(&cid); err != nil {
		return err
	}
	if !i.Force {
		sco, err := x.In("cate_id", append(cid, i.ID)).Count(new(Content))
		if err != nil {
			return err
		}
		if len(cid) > 0 || sco > 0 {
			return resp.Confirm(c, fmt.Sprintf("要删除的分类下，共有%d个下级分类，%d条内容，确认删除?", len(cid), sco))
		}
	} else {
		if _, err := x.In("cate_id", append(cid, i.ID)).Delete(new(Content)); err != nil {
			return err
		}
	}
	if _, err := x.In("id", append(cid, i.ID)).Delete(new(Category)); err != nil {
		return err
	}
	return nil
}

func UpdateCategoryHandler(c echo.Context) error {
	i := new(Category)
	if err := c.Bind(i); err != nil {
		return err
	}
	if i.ID == 0 {
		return errs.Called("分类编号不能为空")
	}
	if i.Type == "" || i.Name == "" {
		return errs.Called("分类的名称和类型不能为空")
	}
	if i.ParentID == 0 {
		i.Path = "/"
	} else {
		p := new(Category)
		if ok, err := x.Where("id = ?", i.ParentID).Get(p); err != nil {
			return err
		} else if !ok {
			return errs.Called("指定的上级分类不存在")
		}
		i.Path = p.Path + strconv.Itoa(p.ID) + "/"
	}
	if l, err := x.ID(i.ID).Update(i); err != nil {
		return err
	} else if l == 0 {
		return errs.Called("更新失败")
	}
	return nil
}

func CreateContentHandler(c echo.Context) error {
	i := new(Content)
	if err := c.Bind(i); err != nil {
		return err
	}
	i.ID = 0
	if i.Type == "" || i.CateID == 0 || i.UserID == 0 || i.Title == "" {
		return errs.Called("内容的标题、类型、分类、创建者不能为空")
	}
	i.CreateTime = time.Now().Unix()
	i.UpdateTime = 0
	if l, err := x.Insert(i); err != nil {
		return err
	} else if l == 0 {
		return errs.Called("插入失败")
	}
	return nil
}

func DeleteContentHandler(c echo.Context) error {
	i := new(struct{
		ID int `json:"id"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	if i.ID == 0 {
		return errs.Called("内容编号不能为空")
	}
	if _, err := x.ID(i.ID).Delete(new(Content)); err != nil {
		return err
	}
	return nil
}

func UpdateContentHandler(c echo.Context) error {
	i := new(Content)
	if err := c.Bind(i); err != nil {
		return err
	}
	if i.ID == 0 {
		return errs.Called("内容编号不能为空")
	}
	if i.Type == "" || i.CateID == 0 || i.UserID == 0 || i.Title == "" {
		return errs.Called("内容的标题、类型、分类、创建者不能为空")
	}
	i.UpdateTime = time.Now().Unix()
	if l, err := x.ID(i.ID).Update(i); err != nil {
		return err
	} else if l == 0 {
		return errs.Called("更新失败")
	}
	return nil
}